//
//  ST_QuestionBodyViewController.swift
//  btvideo
//
//  Created by TT mini on 2022/6/22.
//  Copyright © 2022 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON
import SwifterSwift
import SwiftUI

class ST_QuestionBodyViewController: UIViewController {

    var data_array : [ST_CommonQuestionListModel] = []
    var currentPage : Int = 0
    
    lazy var tableView : UITableView = {
        let t = UITableView()
        t.backgroundColor = .clear
        t.delegate = self
        t.dataSource = self
        t.register(cellWithClass: ST_QuestionsCell.self)
        t.separatorStyle = .none
        t.showsHorizontalScrollIndicator = false
        t.showsVerticalScrollIndicator = false
        t.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: BTSafeAreaBottom + 30 , right: 0)
        if #available(iOS 11.0, *) {
            t.contentInsetAdjustmentBehavior = .never
        }
        return t
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = Color.init(hexString: "#F5F8FA")
        make_u_i()
        bind()
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension ST_QuestionBodyViewController {
    
    func bind() {
        
        TIANSession(requestUrl: PH.commonQuestion, parameters: ["category_id":currentPage.string]).withUnretained(self).subscribe(onNext: { own, json in
            
            switch json {
            case .success(let jsonValue):
                let array = jsonValue["data"].arrayValue.map { (itemJson) -> ST_CommonQuestionListModel in
                    return ST_CommonQuestionListModel.init(fromJson: itemJson)
                }
                own.data_array.removeAll()
                own.data_array = array
                own.tableView.reloadData()
            case .failure(_):break
                
            }
            
        }).disposed(by: rx.disposeBag)
    }
    
    func make_u_i() {
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview()
            make.width.equalTo(ScreenWidth)
            make.bottom.equalToSuperview()
        }
    }
}

extension ST_QuestionBodyViewController : UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data_array.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 83
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: ST_QuestionsCell.self, for: indexPath)
        guard let model = self.data_array[safe: indexPath.row] else {return cell}
        cell.selectionStyle = .none
        cell.configer_with_infos(model: model)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        guard let model = self.data_array[safe: indexPath.row] else {return}
        
        let vc = ST_AnswersViewController()
        vc.titleStr = model.title
        vc.bodyStr = model.content
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

//MARK: - Model

class ST_CommonQuestionListModel : NSObject{

    var id : String
    var title : String
    var content : String
    
    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON){
        
        id = json["id"].stringValue
        title = json["title"].stringValue
        content = json["content"].stringValue
        
        super.init()
    }
}

